const { Sequelize } = require('sequelize');
const path = require('path')
const log4j = require('../utils/log4j')

let MYSQL_DB = 'test';
let MYSQL_PWD = 'hangcon456';
let MYSQL_HOST = '127.0.0.1';

if (process.env.NODE_ENV == 'development') {
  MYSQL_HOST = '127.0.0.1'
}

const sequelize = new Sequelize(MYSQL_DB, 'root', MYSQL_PWD, {
  dialect: 'mysql',
  host: MYSQL_HOST,
  port: 3306,
  models: [
    `${path.resolve(__dirname, '../model/mysql')}/*.js`
  ],
  pool: {
    max: 5,
    min: 0,
    idle: 30000
  },
  dialectOptions: {
    dateStrings: true,
    typeCast: true
  },
  logging: msg => log4j.logInfo(msg)
})

sequelize.authenticate().then(() => {
  console.log('sq数据库连接成功');
}).catch((err) => {
  console.log('sq数据库连接失败', err);
});

module.exports =  sequelize;